﻿
namespace ZHDJ.MobileInterface
{
    public abstract class AbstractBasePage : System.Web.UI.Page
    {
        // 获取供应商编码[道一调用时可以不传该编码]
        protected string SystemCode
        { get; set; }

        protected void ProcessRequest<InParam, OutParam>()
            where InParam : class
            where OutParam : class
        {
            try
            {

                ResponseHelper.WriteResponseMessage(Response, "不加密方式暂不提供");
                return;

                // 获取请求报文
                string strOriRequestMsg = RequestHelper.ReadRequestMessage(Request);

                // 请求入参
                InParam inParam = null;

                if (!string.IsNullOrEmpty(strOriRequestMsg))
                {
                    //try
                    //{
                    //    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(@"E:\NewParty\MobileService\RequestLog.txt", true))
                    //        writer.WriteLine(string.Format("{0}请求报文：[{1}]", System.DateTime.Now, strOriRequestMsg));
                    //}
                    //catch { }

                    //// 解密请求报文
                    //string strDecRequestMsg = CryptoHelper.Decrypt(strOriRequestMsg);

                    // 反序列化请求报文
                    inParam = SerializeHelper.JsonDeserialize<InParam>(strOriRequestMsg);
                }

                // 业务逻辑返回的响应对象
                OutParam outParam = BusinessProcess<InParam, OutParam>(inParam);

                // 序列化响应对象
                string strOriResponseMsg = SerializeHelper.JsonSerializer<OutParam>(outParam);

                //// 加密响应报文
                //string strEncResponseMsg = CryptoHelper.Encrypt(strOriResponseMsg);

                // 返回响应报文
                ResponseHelper.WriteResponseMessage(Response, strOriResponseMsg);
            }
            catch (System.Exception ex)
            {
                // 加密异常响应报文
                string strEncResponseMsg = CryptoHelper.Encrypt("{\"code\": \"0\", \"data\": {}, \"desc\": \"业务处理异常\"}", "");

                // 返回异常响应报文
                ResponseHelper.WriteResponseMessage(Response, strEncResponseMsg);
            }
        }

        private string GetCryptograhpyKey()
        {
            // 获取供应商编码[道一调用时可以不传该编码]
            //SystemCode = Request.QueryString["SystemCode"];  

            //string[] systemCodes = ServiceSettings.SystemCode.Split(';');
            //string[] keys = ServiceSettings.CryptograhpyKey.Split(';');
            //string currentKey = keys[0].PadRight(24, 'a');

            //if (string.IsNullOrEmpty(SystemCode))
            //{ SystemCode = systemCodes[0]; }

            //for(int i = 0; i < systemCodes.Length; i++)
            //{
            //    if (string.Compare(systemCodes[i], SystemCode, true) == 0)
            //    { currentKey = keys[i].PadRight(24, 'a'); }
            //}

            //return currentKey;
            return ServiceSettings.CryptograhpyKey.PadRight(24, 'a');
        }

        protected void SafeProcessRequest<InParam, OutParam>()
            where InParam : class
            where OutParam : class
        {
            string cryptograhpyKey = GetCryptograhpyKey();
 
            try
            {

                // 获取请求报文
                string strOriRequestMsg = RequestHelper.ReadRequestMessage(Request);

                // 请求入参
                InParam inParam = null;

                if (!string.IsNullOrEmpty(strOriRequestMsg))
                {
                    //try
                    //{
                    //    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(@"E:\NewParty\MobileService\RequestLog.txt", true))
                    //        writer.WriteLine(string.Format("{0}请求报文：[{1}]", System.DateTime.Now, strOriRequestMsg));
                    //}
                    //catch { }

                    // 解密请求报文
                    string strDecRequestMsg = CryptoHelper.Decrypt(strOriRequestMsg, cryptograhpyKey);

                    // 反序列化请求报文
                    inParam = SerializeHelper.JsonDeserialize<InParam>(strDecRequestMsg);
                }

                // 业务逻辑返回的响应对象
                OutParam outParam = BusinessProcess<InParam, OutParam>(inParam);

                // 序列化响应对象
                string strOriResponseMsg = SerializeHelper.JsonSerializer<OutParam>(outParam);

                // 加密响应报文
                string strEncResponseMsg = CryptoHelper.Encrypt(strOriResponseMsg, cryptograhpyKey);

                // 返回响应报文
                ResponseHelper.WriteResponseMessage(Response, strEncResponseMsg);
            }
            catch (System.Exception ex)
            {
                // 加密异常响应报文
                string strEncResponseMsg = CryptoHelper.Encrypt("{\"code\": \"0\", \"data\": {}, \"desc\": \"业务处理异常\"}", cryptograhpyKey);

                string urlPara = Request.Url.AbsoluteUri;
                urlPara += ";总参数数目[" + Request.QueryString.Count.ToString() + "]";

                for (int i = 0; i < Request.QueryString.Count; i++ )
                {
                    urlPara += ":" + Request.QueryString[i];
                }

                CommonUtils.InsertMobileInterfaceLog(SystemCode, RequestHelper.ReadRequestMessage(Request), urlPara + ";" + ex.Message + ":" + ex.StackTrace); 
                // 返回异常响应报文
                ResponseHelper.WriteResponseMessage(Response, strEncResponseMsg); 
            }
        }

        protected abstract OutParam BusinessProcess<InParam, OutParam>(InParam inParam)
            where InParam : class
            where OutParam : class;
    }
}